package com.bigegg.employee.api;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bigegg.employee.util.CommonUtil;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.bigegg.employee.domain.Classes;
import com.bigegg.employee.service.IClassesService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 专业Controller
 * 
 * @author 刘博
 * @date 2022-08-11
 */
@RestController
@RequestMapping("/employee/classes")
public class ClassesController extends BaseController
{
    @Autowired
    private IClassesService classesService;

    /**
     * 查询专业列表
     */
    @PreAuthorize("@ss.hasPermi('employee:classes:list')")
    @GetMapping("/list")
    public TableDataInfo list(Classes classes)
    {
        startPage();
        List<Classes> list = classesService.selectClassesList(classes);
        return getDataTable(list);
    }

    /**
     * 导出专业列表
     */
    @PreAuthorize("@ss.hasPermi('employee:classes:export')")
    @Log(title = "专业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Classes classes)
    {
        List<Classes> list = classesService.selectClassesList(classes);
        ExcelUtil<Classes> util = new ExcelUtil<Classes>(Classes.class);
        util.exportExcel(response, list, "专业数据");
    }

    /**
     * 获取专业详细信息
     */
    @PreAuthorize("@ss.hasPermi('employee:classes:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(classesService.selectClassesById(id));
    }

    /**
     * 新增专业
     */
    @PreAuthorize("@ss.hasPermi('employee:classes:add')")
    @Log(title = "专业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Classes classes)
    {
        return toAjax(classesService.insertClasses(classes));
    }

    /**
     * 修改专业
     */
    @PreAuthorize("@ss.hasPermi('employee:classes:edit')")
    @Log(title = "专业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Classes classes)
    {
        return toAjax(classesService.updateClasses(classes));
    }

    /**
     * 删除专业
     */
    @PreAuthorize("@ss.hasPermi('employee:classes:remove')")
    @Log(title = "专业", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(classesService.deleteClassesByIds(ids));
    }

    /**
     * 上传excel
     */
    @PostMapping("/importData")
    public AjaxResult importData(@RequestParam("file") MultipartFile file)
    {

        this.classesService.importData(file);
        return AjaxResult.success();
    }

    /**
     * 下载模板
     */
    @PostMapping("/downloadTpl")
    public void downloadTpl(HttpServletRequest request, HttpServletResponse response)
    {

        // 模板的路径
        String tplPath = "tpl/班级导入模板.xlsx";

        CommonUtil.downloadResource(tplPath,response);

    }
}
